﻿@inject IHttpService _httpService
@inject IDataCacheService _dataCacheService


<MDCard Class="mb-4">
    <div style="display: flex; justify-content: space-around; flex-wrap: wrap; " class="pt-8">
        <div class="ps-4 pe-4" style="max-width: 300px; width: 100%;">
            <CnGalWebSite.Components.Inputs.MasaSelecte Value="ScreenType" ValueChanged="OnScreenTypeChanged" TValue="ExaminedNormalListPassType" Label="筛选" Class="pt-0" />
        </div>
        <div class="ps-4 pe-4" style="max-width: 300px; width: 100%;">
            <CnGalWebSite.Components.Inputs.MasaSelecte Value="OrderType" ValueChanged="OnOrderTypeChangedChanged" TValue="ExaminedNormalListSortType" Label="排序" Class="pt-0" />
        </div>
    </div>
</MDCard>
@if(isReady)
{
    @foreach (var examine in pagedResultDto.Data)
        {
            <MDCard Class="pa-3 mb-3">
                <div style="display: flex; align-items: center;">
                    <div>
                        @if (examine.IsPassed != null)
                        {
                            if (examine.IsPassed == true)
                            {
                                <MIcon Color="success">mdi-checkbox-marked-circle-outline</MIcon>
                            }
                            else
                            {
                                <MIcon Color="error">mdi-alert-circle </MIcon>
                            }
                        }
                        else
                        {
                            <MIcon Color="warning">mdi-history</MIcon>
                        }
                    </div>
                    <div class="ms-3">
                        @{ string name = "";}
                        @if (examine.Type == ExaminedNormalListModelType.Entry)
                        {
                            name = "entries";

                        }
                        else if (examine.Type == ExaminedNormalListModelType.Article)
                        {
                            name = "articles";

                        }
                        else if (examine.Type == ExaminedNormalListModelType.Tag)
                        {
                            name = "tags";

                        }
                        else if (examine.Type == ExaminedNormalListModelType.Disambig)
                        {
                            name = "disambigs";

                        }
                        else if (examine.Type == ExaminedNormalListModelType.Periphery)
                        {
                            name = "peripheries";

                        }
                        else if (examine.Type == ExaminedNormalListModelType.User)
                        {
                            name = "space";

                        }
                        else if (examine.Type == ExaminedNormalListModelType.Video)
                        {
                            name = "videos";

                        }


                        @if (examine.Type is ExaminedNormalListModelType.Comment or ExaminedNormalListModelType.PlayedGame or ExaminedNormalListModelType.FavoriteFolder)
                        {
                            <span>
                                @($"序号『{ examine.Id.ToString("00000")}』")
                                <a  href="@("/home/examines?Id="+examine.Id)">
                                    @examine.Operation.GetDisplayName()
                                </a>
                            </span>
                        }
                        else
                        {
                            <span>

                                @($"序号『{ examine.Id.ToString("00000")}』")
                                对
                                <a  href="@(name+"/index/"+examine.RelatedId)">
                                    @(string.IsNullOrWhiteSpace(examine.RelatedName) ?("Id："+ examine.RelatedId) : examine.RelatedName )
                                </a>
                                进行
                                <a href="@((examine.IsPassed==true&&examine.Type!= ExaminedNormalListModelType.User&&examine.Type!= ExaminedNormalListModelType.Comment)?("/home/editrecord?ContrastId=" +examine.Id):("/examine?Id="+examine.Id))">
                                    @examine.Operation.GetDisplayName()
                                </a>
                                操作
                            </span>
                        }

                        <br />
                        <span class="label-large">
                            @("申请时间："+examine.ApplyTime.ToString("yyyy-MM-dd HH:mm")) &nbsp; &nbsp;
                            @if (examine.PassedTime != null)
                            {
                                @("审核时间："+examine.PassedTime.Value.ToString("yyyy-MM-dd HH:mm"))
                            }

                        </span>
                    </div>
                </div>
            </MDCard>
        }
        @if (pagedResultDto.TotalPages > 1)
        {
            <CnGalWebSite.Components.Paginations.MasaPagination Value="@pagedResultDto.CurrentPage"  Length="@pagedResultDto.TotalPages" ValueChanged="OnPageClick"/>
        }
        @if (pagedResultDto.Data.Count == 0)
        {
            <div class="mb-3">
                <NotFoundCard  Fill/>
            </div>
        }


}
else
{
    <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill/>
}

@code {
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    [Parameter]
    public string Id { get; set; }

    bool isReady;

    public string Sorting { get; set; } = "Id desc";
    public string ScreeningConditions { get; set; } = "All";

    public PagedResultDto<ExaminedNormalListModel> pagedResultDto = new PagedResultDto<ExaminedNormalListModel>();
    public ExaminedNormalListPassType ScreenType { get; set; }
    public ExaminedNormalListSortType OrderType { get; set; }


    public async Task OnScreenTypeChanged(ExaminedNormalListPassType type)
    {
        ScreenType = type;
        switch(type)
        {
            case ExaminedNormalListPassType.All:
                ScreeningConditions = "All";
                break;
            case ExaminedNormalListPassType.Passed:
                ScreeningConditions = "Passed";
                break;
            case ExaminedNormalListPassType.Passing:
                ScreeningConditions = "Passing";
                break;
            case ExaminedNormalListPassType.UnPassed:
                ScreeningConditions = "UnPassed";
                break;
        }
        await OnPageClick(1);
    }


    public async Task OnOrderTypeChangedChanged(ExaminedNormalListSortType type)
    {
        OrderType = type;
        switch (type)
        {
            case  ExaminedNormalListSortType.EditTime:
                Sorting = "Id desc";
                break;
            case ExaminedNormalListSortType.ExamineTime:
                Sorting = "PassedTime desc";
                break;
        }
        await OnPageClick(_dataCacheService.SpaceViewCache.ExaminesCurrentPage);
    }

    protected override async Task OnInitializedAsync()
    {
        if(_dataCacheService.SpaceViewCache.UserId!=Id)
        {
            _dataCacheService.SpaceViewCache.UserId = Id;
            _dataCacheService.SpaceViewCache.ExaminesCurrentPage = 1;
        }
        //获取审核列表
        await OnPageClick(_dataCacheService.SpaceViewCache.ExaminesCurrentPage);
        isReady=true;
    }

    private async Task OnPageClick(int pageIndex)
    {
        _dataCacheService.SpaceViewCache.ExaminesCurrentPage = pageIndex;
        try
        {
            pagedResultDto = await _dataCacheService.UserExaminesDataCatche.GetCache($"?userId={Id}&sorting={Sorting}&maxResultCount={10}&currentPage={pageIndex}&ScreeningConditions={ScreeningConditions}");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取编辑记录失败");
        }
    }

}
